package com.siraz.questica;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;

public class Player
{
	public int direction = 0;
	public String name = "Player";
	public float x = 0f;
	public float y = 0f;
	public Animation legsWalkAnimation;
	public Texture legsWalkSheet;
	public Array<TextureRegion> legsWalkFrames;
	
	public Animation torsoWalkAnimation;
	public Texture torsoWalkSheet;
	public Array<TextureRegion> torsoWalkFrames;
	
	public Texture headTexture;
	public TextureRegion headRegion;
	
	private int currentFrame;

	public float stateTime = 0f;

	public Player() {
		legsWalkFrames = new Array<TextureRegion>(5);
		legsWalkSheet = new Texture(Gdx.files.internal("data/legswalk.png"));
		TextureRegion tmp[][] = TextureRegion.split(legsWalkSheet, 25, 9);
        for (int i = 0; i < 1; i++) {
                for (int j = 0; j < 5; j++) {
                        legsWalkFrames.add(tmp[i][j]);
                }
        }
		legsWalkAnimation = new Animation(0.1f, legsWalkFrames, Animation.LOOP);
		
		torsoWalkFrames = new Array<TextureRegion>(5);
		torsoWalkSheet = new Texture(Gdx.files.internal("data/torwalk.png"));
		TextureRegion tmp2[][] = TextureRegion.split(torsoWalkSheet, 25, 22);
        for (int i = 0; i < 1; i++) {
                for (int j = 0; j < 5; j++) {
                	torsoWalkFrames.add(tmp2[i][j]);
                }
        }
        torsoWalkAnimation = new Animation(0.1f, torsoWalkFrames, Animation.LOOP);
        
        headTexture = new Texture(Gdx.files.internal("data/standhead.png"));
        headRegion = new TextureRegion(headTexture, 25, 22);
	}

	public void draw(SpriteBatch batch) {
		
		if(Gdx.input.isKeyPressed(Input.Keys.A)) {
			x -= 1;
			direction = 0;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.D)) {
			x += 1;
			direction = 1;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.W)) {
			y += 1;
		}
		if(Gdx.input.isKeyPressed(Input.Keys.S)) {
			y -= 1;
		}
		if (direction == 0) {
			//if(!legsWalkFrames.get(currentFrame).isFlipX())
			//	legsWalkFrames.get(currentFrame).flip(true, false);
		}
		else {
			//if(legsWalkFrames.get(currentFrame).isFlipX())
			//	legsWalkFrames.get(currentFrame).flip(false, false);
		}
		
		stateTime += Gdx.graphics.getDeltaTime();
		currentFrame = legsWalkAnimation.getKeyFrameIndex(stateTime);
		batch.draw(legsWalkFrames.get(currentFrame), x, y, 25/2, 0, 25, 9, direction == 0 ? -1 : 1, 1, 0);
		batch.draw(torsoWalkFrames.get(currentFrame), x, y+1, 25/2, 0, 25, 22, direction == 0 ? -1 : 1, 1, 0);
		batch.draw(headRegion, x, y+13, 25/2, 0, 25, 22, direction == 0 ? -1 : 1, 1, 0);
	}
}